export function setLocal(key, value) {
    if (value) {
        localStorage.setItem(key, JSON.stringify(value))
    }
    if (typeof(value) == 'number' && value == 0) {
        localStorage.setItem(key, value)
    }
    if (typeof(value) == 'boolean' && value == false) {
        localStorage.setItem(key, false)
    }
}

export function getLocal(key) {
    var value = localStorage.getItem(key)
    if (value) {
        return JSON.parse(value)
    }

}

export function localClearAll() {
    localStorage.clear();
}

export function initStoreFromLocal() {
    var props = this;
    var l = localStorage.length
    for (var i = 0; i < l; i++) {
        var key = localStorage.key(i)
        var _isTrue = key.split(".").length == 2
        if (_isTrue) {
            var namespace = key.split('.')[0]
            var _key = key.split('.')[1]
            props.$store.commit(`${namespace}/snapShot`, {[_key]: getLocal(`${namespace}.${_key}`)})
        }
    }
}